﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Diagnostics;

namespace RentIt06
{
    public class Log
    {
        private const string FILE_PATH = "C:\\RentItServices\\RentIt06\\";

        private static Log instance;

        public static Log Instance
        {
            get
            {
                return instance ?? (instance = new Log());
            }
        }

        private Log()
        {
            write("Log started.");
        }

        private void write(Object msg)
        {

            var filename = FILE_PATH + "log " + DateTime.Now.ToString("yy-MM-dd") + ".txt";
            var file = !File.Exists(filename) ? File.Create(filename) : File.Open(filename, FileMode.Append);
            var sw = new StreamWriter(file);

            try
            {
                sw.WriteLine(DateTime.Now.ToString("T") + "> " + msg);
            }
            catch (Exception e)
            {
                sw.WriteLine(DateTime.Now.ToString("T") + "> " + e);
            }
            sw.Flush();
            sw.Close();
        }

        public static void Write(Object msg)
        {
            Instance.write(msg);
        }

        [Conditional("DEBUG")]
        public static void Debug(Object msg)
        {
            Instance.write(msg);
        }
    }
}